/* LoadListitemRenderer.java
*/
package org.imdm.portal.loadchecker.ui;

import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.ListitemRendererExt;
import org.zkoss.zul.Popup;
import org.zkoss.zul.Vbox;

/**
 * @author James.Sun
 *
 */
public class LoadListitemRenderer implements ListitemRenderer,ListitemRendererExt{

	public void render(Listitem row, Object data) throws Exception {
		row.getChildren().clear();
		java.util.Map.Entry entry = (java.util.Map.Entry)data;
		LoadInfo li = (LoadInfo)entry.getValue();
		Listcell lcServer = new Listcell(li.getServer());
		Listcell lcLoad = new Listcell(li.getLoad());
		Listcell lcLinker = new Listcell(li.getLinker());
		Listcell lcLinkTime = new Listcell(li.getLinktime());
		String l = "..";
		if ( !li.getLoad().equalsIgnoreCase(LoadInfo._UPDATING) ) {
			if ( li.validLogins() )
				l = ""+li.getLogins()+" ";
			else
				l = LoadInfo._UNKNOWN;
		}
		Listcell lcLogins = new Listcell(l);
		if ( li.validLogins() ) {
			lcLogins.appendChild(getLabelbyCount(li.getLogins()));
			Popup pl = this.getLoginPopup(li);
			if (pl != null) {
				pl.setParent(row.getListbox().getParent());
				lcLogins.setTooltip(pl.getId());
			}
		}

		//create popup tooltip for server name cells
		Popup ps = getServerPopup(li);
		ps.setParent(row.getListbox().getParent());
		lcServer.setTooltip(ps.getId());
//		Image svr_detail = new Image("img/mag16x16.png");
//		svr_detail.setMold("alphafix");
//		svr_detail.setPopup(ps.getId());
//		lcServer.appendChild(svr_detail);
		row.appendChild(lcServer);
		row.appendChild(lcLoad);
		row.appendChild(lcLinker);
		row.appendChild(lcLinkTime);
		row.appendChild(lcLogins);
	}

	private Popup getLoginPopup(LoadInfo li) {
		Popup p = new Popup();
		
		Label lb_sum = new Label();
		lb_sum.setStyle("font-weight:bold");
		if ( li.getLogins() == 0 ) {
			lb_sum.setValue("No user login.");
			p.appendChild(lb_sum);
			return p;
		}
		else {
			lb_sum.setValue("" + li.getLogins() + " user" + ((li.getLogins()>1)?"s":"") 
							+ " with total " + li.getLogin_entities() + " login session"
							+ ((li.getLogin_entities()>1)?"s\n ":"\n "));
			lb_sum.setMultiline(true);
			lb_sum.setPre(true);
		}
		Label lb_names = new Label(li.getLogin_names());
		lb_names.setMultiline(true);
		Vbox v = new Vbox();
		v.appendChild(lb_sum);
		v.appendChild(lb_names);
		p.appendChild(v);
		return p;
	}

	private Popup getServerPopup(LoadInfo li) {
		Popup p = new Popup();
		Vbox v = new Vbox();
		p.appendChild(v);
		Label lb_svr = new Label(li.getServer() + "\n ");
		lb_svr.setMultiline(true);
		lb_svr.setPre(true);
		lb_svr.setStyle("font-weight:bold");
		Label lb_info = new Label(li.getServerDetails());
		lb_info.setMultiline(true);
		v.appendChild(lb_svr);
		v.appendChild(lb_info);
		return p;
	}
	
	private static Label getLabelbyCount(int i) {
		if ( i < 0 ) return new Label();
		StringBuffer _buf = new StringBuffer("_");
		for ( int n = 0; n < i; n++ ) {
			_buf.append("_");
		}
		Label _r = new Label(_buf.toString());
		_r.setStyle("color:#4343DD;background:#4343DD");
		if ( i == 1 )
			_r.setStyle("color:#00CC00;background:#00CC00");
		else if ( i > 1 && i <= 3)
			_r.setStyle("color:#DDCC00;background:#DDCC00");
		else if ( i > 3 && i <= 5)
			_r.setStyle("color:#FF8800;background:#FF8800");
		else if ( i > 5 )
			_r.setStyle("color:#FF3333;background:#FF3333");
		return _r;
	}

	public int getControls() {
		return ListitemRendererExt.DETACH_ON_UNLOAD;
	}

	public Listcell newListcell(Listitem row) {
		return null;
	}

	public Listitem newListitem(Listbox lb) {
		return null;
	}
}
